package com.gh.controller.admin;


import com.gh.common.exception.BusinessException;
import com.gh.common.result.R;
import com.gh.common.result.ResponseEnum;
import com.gh.entity.Dict;
import com.gh.service.DictService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author Eric
 * @since 2021-09-06
 */
@RestController
@RequestMapping("/admin/core/dict")
public class AdminDictController {

    @Resource
    private DictService dictService;

    @ApiOperation("excel数据的批量导入")
    @PostMapping("/import")
    public R importData(@RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            dictService.importData(inputStream);
            return R.ok().message("数据批量导入成功");
        } catch (Exception e) {
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR, e);
        }

    }

    @ApiOperation("数据的批量导出")
    @GetMapping("/exportDictToExcel")
    public R exportDictToExcel(HttpServletResponse response) {

        dictService.exportDictToExcel(response);

        return R.ok().message("数据批量导出成功");
    }

    @ApiOperation("树形数据的延迟加载")
    @GetMapping("/listByParentId/{parentId}")
    public R listByParentId(@PathVariable Long parentId) {
        List<Dict> dictList = dictService.listByParentId(parentId);
        return R.ok().data("list", dictList);
    }
}

